'use client'

import {useState} from 'react'
import type {ProColumns} from '@ant-design/pro-components'
import {ProTable} from '@ant-design/pro-components'
import {Button, message, Modal} from 'antd'
import {PlusOutlined} from '@ant-design/icons'

type ProductItem = {
  id: string
  code: string
  name: string
  category: string
  price: number
  stock: number
  unit: string
  status: string
}

export default function ProductsPage() {
  const [createModalVisible, setCreateModalVisible] = useState(false)

  const columns: ProColumns<ProductItem>[] = [
    {
      title: '商品编码',
      dataIndex: 'code',
      copyable: true,
      width: 120,
    },
    {
      title: '商品名称',
      dataIndex: 'name',
      width: 200,
    },
    {
      title: '类别',
      dataIndex: 'category',
      width: 120,
    },
    {
      title: '单价',
      dataIndex: 'price',
      width: 120,
      valueType: 'money',
    },
    {
      title: '库存',
      dataIndex: 'stock',
      width: 100,
    },
    {
      title: '单位',
      dataIndex: 'unit',
      width: 80,
    },
    {
      title: '状态',
      dataIndex: 'status',
      width: 100,
      valueEnum: {
        active: { text: '正常', status: 'Success' },
        inactive: { text: '停售', status: 'Error' },
      },
    },
    {
      title: '操作',
      width: 180,
      key: 'option',
      valueType: 'option',
      render: (_, record) => [
        <a
          key="edit"
          onClick={() => {
            message.info('编辑功能开发中')
          }}
        >
          编辑
        </a>,
        <a
          key="delete"
          onClick={() => {
            Modal.confirm({
              title: '确认删除',
              content: '确定要删除这个商品吗？',
              onOk: () => {
                message.success('删除成功')
              },
            })
          }}
        >
          删除
        </a>,
      ],
    },
  ]

  const mockData: ProductItem[] = [
    {
      id: '1',
      code: 'P001',
      name: 'iPhone 15 Pro Max',
      category: '手机',
      price: 9999,
      stock: 100,
      unit: '台',
      status: 'active',
    },
    {
      id: '2',
      code: 'P002',
      name: '华为 Mate 60 Pro',
      category: '手机',
      price: 8999,
      stock: 50,
      unit: '台',
      status: 'active',
    },
  ]

  return (
    <ProTable<ProductItem>
      columns={columns}
      dataSource={mockData}
      rowKey="id"
      search={{
        labelWidth: 120,
      }}
      toolBarRender={() => [
        <Button
          key="button"
          icon={<PlusOutlined />}
          type="primary"
          onClick={() => setCreateModalVisible(true)}
        >
          新建
        </Button>,
      ]}
    />
  )
} 